package com.nd.mobile.study.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.domain.News;

public class HomeExpandListAdapter extends BaseExpandableListAdapter{

	String[] groups = new String[] { "资讯"};
	public int[] tags = new int[] { 0,0 };
	
	private Context mContext;
	
	private News[] mData;

	public HomeExpandListAdapter(Context mContext) {
		super();
		this.mContext = mContext;
	}

	public void setData(News[] data){
		this.mData = data;
	}
	
	public News[] getData(){
		return mData;
	}
	
	@Override
	public int getGroupCount() {
		return groups.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
//		if(groupPosition == 0){
//			return 5;
//		}
//		return 1;
		return null == mData ? 0 : mData.length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mData[childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	class GroupHolder {
		ImageView img;
		TextView title;
	}
	
	class ChildHolder {
		TextView title;
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolder groupHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.list_group_item, null);
			groupHolder = new GroupHolder();
			groupHolder.img = (ImageView) convertView.findViewById(R.id.tag_img);
			groupHolder.title = (TextView) convertView
					.findViewById(R.id.title_view);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		if (tags[groupPosition] == 0) {
			groupHolder.img.setImageResource(R.drawable.item_arrow);
		} else {
			groupHolder.img.setImageResource(R.drawable.item_arrow);
		}
		groupHolder.title.setText(groups[groupPosition]);

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (groupPosition == 0) {
			ChildHolder childHolder = null;
			if(null == convertView || null == convertView.getTag()){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.news_list_item, null);
				
				childHolder = new ChildHolder();
				childHolder.title = (TextView) convertView.findViewById(R.id.news_catalog_title);
				convertView.setTag(childHolder);
			}else{
				childHolder = (ChildHolder) convertView.getTag();
			}
			childHolder.title.setText(mData[childPosition].getTitle());
		}
		if (groupPosition == 1) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.app_grid_item, null);

			ImageView icon = (ImageView) convertView.findViewById(R.id.app_grid_icon);
			TextView appNameTv = (TextView) convertView.findViewById(R.id.app_grid_name);
			icon.setBackgroundResource(R.drawable.ic_launcher);
			appNameTv.setText("海西教育网");
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
